home *** CD-ROM | disk | FTP | other *** search
/ Interactive Web Graphics with Shout 3D / Interactive Web Graphics With Shout 3D.iso / mac / Code / Chapter Code.exe / Chapter08 / TranslateXYPanel.java < prev    next >
Text File  |  2000-06-27  |  2KB  |  91 lines

  1.  
  2. package applets;
  3.  
  4. import shout3d.*;
  5. import shout3d.core.*;
  6. import shout3d.math.*;
  7.  
  8.  
  9. public class TranslateXYPanel extends Shout3DPanel implements DeviceObserver{
  10.     
  11.     Transform boxTrans;
  12.     float[] worldPos = new float[3];
  13.     int pixelStartX;
  14.     int pixelStartY;
  15.     int pixelEndX;
  16.     int pixelEndY;
  17.  
  18.  
  19.     public TranslateXYPanel (Shout3DApplet applet){
  20.         super(applet);
  21.     }
  22.     
  23.         public void customInitialize() {
  24.         addDeviceObserver(this,"MouseInput", null);
  25.  
  26.         boxTrans = (Transform) getNodeByName("mytrans");
  27.  
  28.         //get reference to position array
  29.         //from the transform
  30.         //and store in worldPos
  31.         worldPos = boxTrans.translation.getValue();
  32.  
  33.         //print out values in worldPos
  34.         //to Java Console
  35.         System.out.println("Box's x position is " + worldPos[0]);
  36.         System.out.println("Box's y position is " + worldPos[1]);
  37.         System.out.println("Box's z position is " + worldPos[2]);        
  38.     }
  39.  
  40.  
  41.  
  42.     protected void finalize()  { 
  43.         removeDeviceObserver(this,"MouseInput");
  44.  
  45.     }
  46.  
  47.  
  48.     public boolean onDeviceInput(DeviceInput di, Object userData) {
  49.         MouseInput mi = (MouseInput) di;
  50.  
  51.         switch (mi.which){
  52.             case MouseInput.DOWN:
  53.                 pixelStartX = mi.x;
  54.                 pixelStartY = mi.y;
  55.                 return true;                                        
  56.  
  57.             case MouseInput.DRAG:
  58.                 pixelEndX = mi.x;
  59.                 pixelEndY = mi.y;
  60.                 
  61.                 //get pixel distances dragged
  62.                 int dragDistanceX = pixelEndX - pixelStartX;
  63.                 int dragDistanceY = pixelEndY - pixelStartY;
  64.  
  65.                 //convert pixel distance to meters
  66.                 // at 50 pixels/1 meter
  67.                 float deltaX = dragDistanceX/50f;
  68.                 float deltaY = -(dragDistanceY/50f);
  69.  
  70.                 //add deltas to current X and Y 
  71.                 //to get new position                
  72.                 worldPos[0] = worldPos[0] + deltaX;
  73.                 worldPos[1] = worldPos[1] + deltaY;
  74.  
  75.                 //put the updated position array
  76.                 //in the transform node.
  77.                 boxTrans.translation.setValue(worldPos);
  78.  
  79.                 //reset the starting pixel for next drag
  80.                 pixelStartX = pixelEndX;
  81.                 pixelStartY = pixelEndY;
  82.                 
  83.                 return true;
  84.  
  85.         }//end of switch
  86.  
  87.         return false;        
  88.     }
  89.  
  90.  
  91. } //end of class